from kisp import *
import random
import sys
import os

class UPS(KISP):
	Type = "Sensor"
	Name = "UPS"
	Description = "UPS Monitor"	
	Arguments = ["NUT UPS Address ( UPS_ID@hostname[:port] )"]	
	Units = "Charge"

	def Start(self,arguments):
		try:
			p = os.popen("upsc %s ups.status" % arguments[0])
			status = p.read()[0:-1]
			
			p = os.popen("upsc %s battery.charge" % arguments[0])
			charge = p.read()[0:-1]
			
			if status == "OL":
				return [0, charge,""]
			else:
				return [1, charge,""]
		except:
			return [-1,0,"Plugin Failed: %s - %s" % (sys.exc_info()[0],sys.exc_info()[1])]
